package com.liang.poi.controller;

import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.config.Configure;
import com.deepoove.poi.plugin.table.LoopRowTableRenderPolicy;
import com.deepoove.poi.xwpf.NiceXWPFDocument;
import com.liang.poi.entity.People;
import com.liang.poi.entity.Student;
import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
import org.apache.poi.xwpf.usermodel.XWPFDocument;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.*;

public class poiPrint {
    public static void main(String[] args)  throws  Exception{
        //模板地址
        String templateFilePath = "C:\\Users\\86151\\Desktop\\study\\poi\\";
        //生成文件的保存地址
        String destFilePath = "C:\\Users\\86151\\Desktop\\study\\poi\\";
        Map<String, Object> datas = new HashMap<String, Object>();

        Student student1 = new Student();
        student1.setAge("12");
        student1.setIndex("1");
        student1.setName("张三");
        student1.setSex("男");
        Student student2 = new Student();
        student2.setAge("13");
        student2.setIndex("2");
        student2.setName("李四");
        student2.setSex("男1");
        List<Student> studentList = Arrays.asList(student1);

        People people1 = new People();
        people1.setAge("13");
        people1.setIndex("1");
        people1.setName("lisi");
        people1.setNote("测试");
        People people2 = new People();
        people2.setAge("14");
        people2.setIndex("2");
        people2.setName("lisi11");
        people2.setNote("测试11");
        ArrayList a = new ArrayList();
        a.add(1);
        a.add(2);
        people2.setAxisNoteList(a);
        List<People> peopleList = Arrays.asList(people1,people2);
        datas.put("lists2",peopleList);

        datas.put("lists",studentList);

        // 行循环实例
        LoopRowTableRenderPolicy policy = new LoopRowTableRenderPolicy();

        //这里可以指定一个config类，用来指定一些规则，
        Configure config = Configure.builder()
                .bind("lists",policy)
                .bind("lists2", policy).build();
        FileInputStream fileInputStream= new FileInputStream(templateFilePath + "lwh.docx");
        XWPFWordExtractor docx = new XWPFWordExtractor(new XWPFDocument(fileInputStream));
        String text = docx.getText();
        String[] split = text.split("[|]");
        XWPFTemplate compile = XWPFTemplate.compile(templateFilePath + "lwh.docx",config);
        compile.render(datas);
        try {
            Date date = new Date();
            compile.writeToFile(destFilePath +"out_test2.docx");
            System.out.println("！！！！！！成功打印模板到"+destFilePath);

        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }
}
